import BaseUtil from '@/BaseUtil';
import { fetterConfig, fetterConfigTable } from './config';
import SelectUtil from '@/SelectUtil';
import BuffUtil from '@/BuffUtil';
import ObjectPool from '@/ObjectPool';
import { Frame } from '@/frame';

export default class ClassFetter {
    static 职业羁绊计数: { [id: string]: number } = {};
    static 技能计数: { [id: string]: number } = {};
    static 所有职业: string[] = ['战士', '法师', '牧师', '骑士', '术士', '盗贼', '游侠', '萨满', '德鲁伊', '死骑', '武僧', '死灵法师'];
    static 提示信息: { [index: string]: string } = {};
    static 缓存池 = new ObjectPool<Frame[]>(ClassFetter.创建BuffUI);
    static 创建BuffUI() {
        let size = 0.025;
        let art = Frame.createBackDrop().setSize(size, size).setTexture('');
        let stack = Frame.createTEXT(art.current).setSize(size, size).setAllPoints(art.current).setText('').setFont(0.01, 'fonts\\fonts.ttf');
        stack.setTextAlignment(TEXTALIGN_BOTTOMRIGHT);
        stack.setOnMouseEnter(ClassFetter.显示提示);
        stack.setOnMouseLeave(ClassFetter.隐藏提示);
        const result = [art, stack];
        return result;
    }
    static 隐藏提示() {
        UIManager.CommonUITip.hide();
    }
    static 显示提示() {
        let h = DzGetTriggerUIEventFrame();
        UIManager.CommonUITip.show(ClassFetter.提示信息['f' + h]);
    }
    constructor() {
        ClassFetter.初始化职业羁绊计数();
        for (let key in fetterConfigTable) {
            BuffUtil.registerBuffType(fetterConfigTable[key]);
        }
        BaseUtil.onTimer(2, ClassFetter.刷新羁绊层数);

        se.on('hero_skill_level_up', ClassFetter.onHeroSkillLevelUp);
        se.on('hero_skill_forget', ClassFetter.onHeroSkillForget);
    }
    static onHeroSkillLevelUp(this: void, { id }) {
        ClassFetter.增加职业技能羁绊(id);
    }
    static onHeroSkillForget(this: void, { id }) {
        ClassFetter.减少职业技能羁绊(id);
    }
    static 刷新羁绊层数() {
        for (let i = 0; i < 6; i++) {
            SelectUtil.forPlayerUnits(ClassFetter.刷新buff, i);
        }
        return true;
    }
    static 刷新buff(u) {
        for (let key in ClassFetter.职业羁绊计数) {
            let val = ClassFetter.职业羁绊计数[key];
            let buffId = fetterConfigTable[key];
            if (val > 0) {
                let buff = BuffUtil.addBuff(u, buffId.id, null);
                buff.stackCount = val;
            } else {
                if (BuffUtil.hasUnitBuff(u, buffId.id)) BuffUtil.clearUnitBuff(u, buffId.id);
            }
        }
    }
    static 增加职业技能羁绊(id: string) {
        if (!ClassFetter.技能计数[id]) {
            ClassFetter.技能计数[id] = 1;
        } else {
            ClassFetter.技能计数[id]++;
        }
        ClassFetter.统计职业羁绊数量();
    }
    static 减少职业技能羁绊(id: string) {
        if (ClassFetter.技能计数[id]) {
            ClassFetter.技能计数[id]--;
            if (ClassFetter.技能计数[id] == 0) {
                delete ClassFetter.技能计数[id];
            }
        }
        ClassFetter.统计职业羁绊数量();
    }
    static 统计职业羁绊数量() {
        ClassFetter.初始化职业羁绊计数();
        for (let id in ClassFetter.技能计数) {
            let zy = ClassFetter.获取技能对应职业(id);
            if (zy) ClassFetter.职业羁绊计数[zy]++;
        }
        ClassFetter.刷新羁绊ui();
    }
    static 获取技能对应职业(id: string) {
        for (let key in fetterConfig) {
            for (let skid of fetterConfig[key]) {
                if (id == skid) {
                    return key;
                }
            }
        }
        return null;
    }
    static 初始化职业羁绊计数() {
        for (let item of ClassFetter.所有职业) {
            ClassFetter.职业羁绊计数['jb_' + item] = 0;
        }
    }
    static 刷新羁绊ui() {
        ClassFetter.提示信息 = {};
        let 池 = ClassFetter.缓存池;
        池.setAllIdleStatus(true);
        let index = 0;
        let x位置 = 0.765;
        for (let key in ClassFetter.职业羁绊计数) {
            let val = ClassFetter.职业羁绊计数[key];
            if (val > 0) {
                let ui组 = 池.borrowObject();
                let [图标ui, 层数ui] = ui组;
                图标ui.setVisible(true);
                图标ui.setTexture(fetterConfigTable[key].art);
                图标ui.setAbsPoint(FRAMEPOINT_BOTTOMLEFT, x位置 - index * 0.027, 0.545);
                层数ui.setText(val + '');
                ClassFetter.提示信息['f' + 层数ui.current] = fetterConfigTable[key].ubertip;
                index++;
            }
        }
        池.forDirtyObjects((frames) => {
            frames[0].visible = false;
        });
    }
}
